﻿//生产领料
function designAndPrintData(LODOP, config) {
	// LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", 1);
	var i, j, k;
	var totalPages = Math.ceil(config.Detail.Details.length / config.Config.psize);
	var left, lastLeft;
	var top, lastTop;
	var detailHeight = 4.5;
	var headerHeight = 8.4;
	var sumHeight = 7.2;
	var detailWidth = 0;
	var sumColWidth = 0;
	for (i = 0; i < config.Config.columns.length; i++) {
		detailWidth += config.Config.columns[i].width;
	}
	var colSum;
	var pidx;
	var detailIndex;
	var detailLeft = 0;//最左边初始坐标
	for (pidx = 0; pidx < totalPages; pidx++) {
		LODOP.ADD_PRINT_TEXT("0mm", "0mm", "210mm", "6.61mm", config.Config.title);
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 14);
		LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
		LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
		//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);

		LODOP.ADD_PRINT_TEXT("9.79mm", "1.59mm", "17.28mm", "4.34mm", "领料仓库:");
		//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
		LODOP.ADD_PRINT_TEXT("9.79mm", "17.99mm", "18.33mm", "4.34mm", config.Detail.WName);
		//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
		LODOP.ADD_PRINT_TEXT("9.79mm", "40.11mm", "17.4mm", "4.34mm", "领料单号:");
		//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
		LODOP.ADD_PRINT_TEXT("9.79mm", "57.16mm", "32mm", "4.34mm", config.Detail.SNo);
		//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
		LODOP.ADD_PRINT_TEXT("9.79mm", "90.11mm", "17.4mm", "4.34mm", "生产单号:");
		//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
		LODOP.ADD_PRINT_TEXT("9.79mm", "106.16mm", "32mm", "4.34mm", config.Detail.OrderSNo);
		//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);

		LODOP.ADD_PRINT_TEXT("9.79mm", "131.18mm", "23.35mm", "4.34mm", "页码:" + (pidx + 1) + "/" + totalPages);
		LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
		//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);

		LODOP.ADD_PRINT_TEXT("9.79mm", "152.1mm", "17.04mm", "4.34mm", "开单时间:");
		LODOP.ADD_PRINT_TEXT("9.79mm", "168.28mm", "39.95mm", "4.34mm", config.Detail.AddDate);

		top = 15.61;
		lastTop = top;
		left = detailLeft;//表头最左边位置
		for (i = 0; i < config.Config.columns.length; i++) {
			if (i === 0) {
				LODOP.ADD_PRINT_LINE(top.toFixed(2) + "mm", left.toFixed(2) + "mm", top.toFixed(2) + "mm", (left + detailWidth).toFixed(2) + "mm", 0, 1);//输出表头顶部线
			}
			LODOP.ADD_PRINT_TEXT((top + 3).toFixed(2) + "mm", left.toFixed(2) + "mm", config.Config.columns[i].width + "mm", headerHeight + "mm", config.Config.columns[i].name);
			LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);//水平居中
			LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
			left += config.Config.columns[i].width;
		}
		//输出表头底部线
		top += headerHeight;
		for (i = 0; i < config.Config.sums.length; i++) {
			config.Config.sums[i].total = 0;
		}
		for (i = 0; i < config.Config.psize; i++) {
			detailIndex = pidx * config.Config.psize + i;
			left = detailLeft;//明细最左边位置，同表头最左边位置
			if (detailIndex < config.Detail.Details.length) {
				for (j = 0; j < config.Config.columns.length; j++) {
					if (j === 0) {
						LODOP.ADD_PRINT_LINE(top.toFixed(2) + "mm", left.toFixed(2) + "mm", top.toFixed(2) + "mm", (left + detailWidth).toFixed(2) + "mm", 0, 1);//输出明细顶部线
					}
					if (config.Config.columns[j].align === 2) {//居中不设置左边缩进
						LODOP.ADD_PRINT_TEXT((top + 1).toFixed(2) + "mm", left.toFixed(2) + "mm", config.Config.columns[j].width + "mm", detailHeight + "mm", config.Detail.Details[detailIndex][config.Config.columns[j].col]);
						LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
					} else if (config.Config.columns[j].align === 3) {//向右减少距离
						LODOP.ADD_PRINT_TEXT((top + 1).toFixed(2) + "mm", (left - 0.8).toFixed(2) + "mm", config.Config.columns[j].width + "mm", detailHeight + "mm", config.Detail.Details[detailIndex][config.Config.columns[j].col]);
						LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);
					} else {
						LODOP.ADD_PRINT_TEXT((top + 1).toFixed(2) + "mm", (left + 0.8).toFixed(2) + "mm", config.Config.columns[j].width + "mm", detailHeight + "mm", config.Detail.Details[detailIndex][config.Config.columns[j].col]);
					}
					if (config.Config.columns[j].size) {
						LODOP.SET_PRINT_STYLEA(0, "FontSize", config.Config.columns[j].size);
					}
					left += config.Config.columns[j].width;
				}
				for (j = 0; j < config.Config.sums.length; j++) {
					if (config.Config.sums[j].col) {
						config.Config.sums[j].total += config.Detail.Details[detailIndex][config.Config.sums[j].col];
					}
				}
			} else {//输出空白行
				LODOP.ADD_PRINT_LINE(top.toFixed(2) + "mm", left.toFixed(2) + "mm", top.toFixed(2) + "mm", (left + detailWidth).toFixed(2) + "mm", 0, 1);//输出明细顶部线
			}
			top += detailHeight;
		}
		left = detailLeft;//明细最左边位置，同表头最左边位置
		LODOP.ADD_PRINT_LINE(lastTop.toFixed(2) + "mm", left.toFixed(2) + "mm", top.toFixed(2) + "mm", left.toFixed(2) + "mm", 0, 1);//表格最左边线（不包括汇总行）
		LODOP.ADD_PRINT_LINE(top.toFixed(2) + "mm", left.toFixed(2) + "mm", top.toFixed(2) + "mm", (left + detailWidth).toFixed(2) + "mm", 0, 1);//最后一行明细底部线
		for (i = 0; i < config.Config.sums.length; i++) {
			config.Config.sums[i].colspan = config.Config.sums[i].colspan || 1;
			if (i > 0) {
				config.Config.sums[i].colend = config.Config.sums[i - 1].colend + config.Config.sums[i].colspan;
			} else {
				config.Config.sums[i].colend = config.Config.sums[i].colspan - 1;
			}
			config.Config.columns[config.Config.sums[i].colend].colend = true;//是否画汇总列右边线
		}
		for (i = 0; i < config.Config.columns.length; i++) {
			left += config.Config.columns[i].width;
			LODOP.ADD_PRINT_LINE(lastTop.toFixed(2) + "mm", left.toFixed(2) + "mm", (config.Config.columns[i].colend ? top + sumHeight : top).toFixed(2) + "mm", left.toFixed(2) + "mm", 0, 1);//所有明细列的右边线（包括汇总行）
		}

		if (config.Config.sums.length > 0) {
			left = detailLeft;//明细最左边位置，同表头最左边位置
			LODOP.ADD_PRINT_LINE(top.toFixed(2) + "mm", left.toFixed(2) + "mm", (top + sumHeight).toFixed(2) + "mm", left.toFixed(2) + "mm", 0, 1);//表格汇总行最左边线


			for (j = 0; j < config.Config.sums.length; j++) {
				k = 0;
				sumColWidth = 0;
				while (k < config.Config.sums[j].colspan) {
					sumColWidth += config.Config.columns[config.Config.sums[j].colend - k].width;
					k += 1;
				}
				//colSum = LODOP.FORMAT("UpperMoney", config.Config.sums[j].total.toFixed(2));
				colSum = getChineseFormat(config.Config.sums[j].total.toFixed(2));
				LODOP.ADD_PRINT_TEXT((top + 2.2).toFixed(2) + "mm", left.toFixed(2) + "mm", sumColWidth + "mm", sumHeight + "mm", config.Config.sums[j].value.replace(new RegExp("{PAGESUM}", "g"), config.Config.sums[j].total.toFixed(2)).replace(new RegExp("{PAGESUMCHINESE}", "g"), colSum));
				if (config.Config.sums[j].align) {
					LODOP.SET_PRINT_STYLEA(0, "Alignment", config.Config.sums[j].align);
				}
				left += sumColWidth;
			}


			top += sumHeight;
			left = detailLeft;//明细最左边位置，同表头最左边位置
			LODOP.ADD_PRINT_LINE(top.toFixed(2) + "mm", left.toFixed(2) + "mm", top.toFixed(2) + "mm", (left + detailWidth).toFixed(2) + "mm", 0, 1);//表格汇总行底部线
		}

		top += 1;

		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "13.54mm", "4.34mm", "开单人:");
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "12.7mm", "15.79mm", "4.34mm", config.Detail.AddManName);
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "39.69mm", "13.54mm", "4.34mm", "领料人:");
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "52.12mm", "15.79mm", "4.34mm", "");

		//if (config.Config.footer) {
		//	top += 3.97;
		//	LODOP.ADD_PRINT_TEXT(top + "mm", "0mm", "40.48mm", "4.34mm", config.Config.footer);
		//}

		LODOP.NEWPAGE();
	}
}

function printInit(printTplConfig, tplData, action) {
	if (!printTplConfig.SvrHost) {
		printTplConfig.SvrHost = "http://localhost:8000";
	}
	jQuery.ajax({
		url: printTplConfig.SvrHost + (printTplConfig.SvrHost.substr(-1) === "/" ? "" : "/") + "CLodopfuncs.js?priority=" + new Date().getTime(),
		dataType: "script",
		success: function (resp) {
			setTimeout(function () {
				var LODOP;
				try {
					LODOP = getCLodop();
					if (!LODOP) {
						layer && layer.open({
							type: 1,
							title: false,
							closeBtn: 0,
							shadeClose: true,
							content: "<font color='#FF00FF'>Web打印服务CLodop未安装启动，点击<a href='CLodop_Setup_for_Win32NT.exe' target='_blank'>这里</a>下载安装（若已安装过，可点击<a href='CLodop.protocol:setup' target='_self'>这里</a>直接启动），成功后请刷新本页面。</font>"
						});
						return;
					}
					console.log('LODOP.CVERSION:', LODOP.CVERSION);
					if (LODOP.CVERSION < "3.0.8.3") {
						layer && layer.open({
							type: 1,
							title: false,
							closeBtn: 0,
							shadeClose: true,
							content: "<br><font color='#FF00FF'>Web打印服务CLodop需升级!点击<a href='CLodop_Setup_for_Win32NT.exe' target='_blank'>这里</a>执行升级,升级成功后请刷新本页面。</font>"
						});
					}
					//===如下空白位置适合调用统一功能(如注册语句、语言选择等):==
					LODOP.SET_LICENSES("杭州感知科技有限公司", "89600FD5AB0D66747807D05CE0721A65", "", "");
					//=======================================================
				} catch (err) {
					alert("打印控件初始化失败:" + err);
				}
				LODOP.PRINT_INIT("");//必须打印初始化一下  
				//LODOP.SET_PRINT_PAGESIZE(1, 2410, 955, "");////设定打印页面大小：宽度、高度固定
				LODOP.SET_PRINT_PAGESIZE(3, 2410, 955, "");////设定打印页面大小：宽度固定、高度自适应
				var printConfig = {};
				var i;
				for (i = 0; i < tplData.Details.length; i++) {
					tplData.Details[i].MaterialSpecModel = (tplData.Details[i].MaterialSpec || "") + " " + (tplData.Details[i].MaterialModel || "");
					tplData.Details[i].ExpDate = tplData.Details[i].ExpDate ? tplData.Details[i].ExpDate.substr(0, 10) : "";
				}
				printConfig.Detail = tplData;
				var columns = [];
				//columns.push({ name: "货号", col: "MaterialCode", width: 26, size: "", bold: false, align: 2 });//1--左靠齐 2--居中 3--右靠齐
				columns.push({ name: "品名", col: "MaterialName", width: 64, size: "", bold: false, align: 1 });
				columns.push({ name: "规格型号", col: "MaterialSpecModel", width: 36, size: "", bold: false, align: 1 });
				columns.push({ name: "生产厂家", col: "MaterialProducer", width: 36, size: "", bold: false, align: 1 });
				columns.push({ name: "数量", col: "Amount", width: 16, size: "", bold: true, align: 2 });
				columns.push({ name: "单位", col: "MaterialUnit", width: 10, size: "", bold: false, align: 2 });
				columns.push({ name: "批号", col: "LotNo", width: 17, size: "", bold: false, align: 1 });
				columns.push({ name: "效期", col: "ExpDate", width: 18.25, size: "", bold: false, align: 1 });
				//columns.push({ name: "单价", col: "Cost", width: 18, size: "", bold: false, align: 3 });
				//columns.push({ name: "金额", col: "TotalCost", width: 18, size: "", bold: false, align: 3 });
				var sumColumns = [];
				//sumColumns.push({ value: "合计金额：￥" + tplData.total.toFixed(2) + "（大写：" + getChineseFormat(printConfig.total.toFixed(2)) + "）", colspan: 3, total: 0 });
				//sumColumns.push({ value: "本页小计", colspan: 3, align: 3, total: 0 });
				//sumColumns.push({ value: "{PAGESUM}", col: "totalvip", align: 3, total: 0 });
				printConfig.Config = { title: printTplConfig.Title, psize: printTplConfig.PageSize, phone: printTplConfig.Phone, address: printTplConfig.DetailAddress, columns: columns, sums: sumColumns, footer: "注：无质量问题概不退货" };
				designAndPrintData(LODOP, printConfig);
				switch (action) {
					case "DESIGN":
						LODOP.PRINT_DESIGN(); //打印设计
						break;
					case "SETUP":
						LODOP.PRINT_SETUP(); //打印维护
						break;
					case "PREVIEW":
						LODOP.PREVIEW(); //打印预览
						break;
					default:
						LODOP.PRINT(); //直接打印
						break;
				}
				// LODOP.SET_PREVIEW_WINDOW(0, 0, 0,1024 , 700, "预览查看.开始打印");
				// LODOP.SET_SHOW_MODE("LANDSCAPE_DEFROTATED", 1);//横向时的正向显示
				// LODOP.SET_SHOW_MODE("SETUP_ENABLESS", 11111111000001);
			}, 100);
		},
		error: function (xhr, err, ex) {
			alert("连接打印服务器失败:" + err);
		}
	});
}